﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace SmartDeviceProject1
{
     public partial class ImageButton : Control

    {
        #region - Variables -

        private bool isPushed;                  // 是否按下标志

        private Bitmap bmpOffscreen;      // 内存绘制图片

        private bool isFocused;                 // 是否获得焦点

        #endregion

        #region - Construct -

        public ImageButton()
        {
            InitializeComponent();

            isPushed = false;
        }

        #endregion

        #region - Property -

        private Image _image;
        /// <summary>
        /// 按键背景图.
        /// </summary> 
        /// <value>The image.</value>
        public Image Image 
        { 
            get { return _image; } 
            set { _image = value; }
        } 
         private string _caption;
         /// <summary> 
         /// 设置按键文字. 
         /// </summary> 
         /// <value>The caption.</value>
         public string Caption 
         { 
             get { return _caption; } 
             set { _caption = value; } 
         }
        #endregion

        #region - Override Events - 
         /// <summary> 
         /// 引发重新绘制事件 
         /// </summary> 
         /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs"/>。</param>
         protected override void OnPaint(PaintEventArgs e)
         { 
             Graphics gx0ff; 
             Rectangle imgRect;
             Brush backBrush; 
             if (bmpOffscreen == null)
                 bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
             gx0ff = Graphics.FromImage(bmpOffscreen); 
             gx0ff.Clear(BackColor); 
             if (!isPushed) 
                 // 如果按下，则变背景色为浅灰 
                 backBrush = new SolidBrush(Parent.BackColor);
             else backBrush = new SolidBrush(Color.LightGray); 
             gx0ff.FillRectangle(backBrush, ClientRectangle); 
             if (Image != null) 
             { 
                 // 将图片放置于控件中心位置 
                 int imageLeft = (this.Width - Image.Width) / 2;
                 int imageTop = (this.Height - Image.Height) / 2;
                 if (!isPushed) // 如果按下，图片左移一个像素点
                 {
                     imgRect = new Rectangle(imageLeft, imageTop, Image.Width, Image.Height); 
                 }
                 else 
                 { 
                     imgRect = new Rectangle(imageLeft + 1, imageTop + 1, Image.Width, Image.Height); 
                 } 
                 // 图片颜色调整（设置透明色） 
                 ImageAttributes imageAttr = new ImageAttributes();
                 //imageAttr.SetColorKey(BackgroundImageColor(Image), BackgroundImageColor(Image)); 

                 imageAttr.SetColorKey(Color.FromArgb(204, 217, 233), Color.FromArgb(204, 217, 233));
                 gx0ff.DrawImage(Image, imgRect, 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, imageAttr); 
             } 
             if (isPushed)
             { 
                 Rectangle rc = this.ClientRectangle; 
                 rc.Width--;
                 rc.Height--;
                 gx0ff.DrawRectangle(new Pen(Color.Black), rc);
             }
             if (Focused) 
             {
                 Rectangle rc = this.ClientRectangle; 
                 rc.Width--; 
                 rc.Height--;
                 gx0ff.DrawRectangle(new Pen(SystemColors.HotTrack), rc);
             } 
             e.Graphics.DrawImage(bmpOffscreen, 0, 0);
             base.OnPaint(e);
         } 
         protected override void OnPaintBackground(PaintEventArgs e) 
         { 
             //Do nothing 
         } 
         /// <summary> 
         /// 引发鼠标按下事件。 
         /// </summary>
         /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"/>。</param> 
         protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) 
         {
             isPushed = true;
             Invalidate(); 
             base.OnMouseDown(e); 
         } 
         /// <summary> /// 引发鼠标抬起事件。 /// </summary> 
         /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.MouseEventArgs"/>。</param>
         protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) 
         { 
             isPushed = false;
             Invalidate();
             base.OnMouseUp(e);
         } 
         /// <summary> /// 引发按键按下事件。 /// </summary> 
         /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyPressEventArgs"/>。</param> 
         protected override void OnKeyPress(KeyPressEventArgs e) 
         { 
             isPushed = true; 
             Invalidate();
             base.OnKeyPress(e); 
         } 
         /// <summary> /// 引发按键抬起事件。 /// </summary>
         /// /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.KeyEventArgs"/>。</param> 
         protected override void OnKeyUp(KeyEventArgs e) 
         { 
             isPushed = false;
             Invalidate();
             base.OnKeyUp(e);
         }
         protected override void OnGotFocus(EventArgs e)
         { 
             base.OnGotFocus(e);
             Invalidate();
         } 
         protected override void OnLostFocus(EventArgs e) 
         {
             base.OnLostFocus(e); 
             Invalidate();
         }
        #endregion

        #region - Methods - #region - Private Methods - 
         /// <summary> /// 获取背景图片底色. /// </summary> 
         /// <param name="image">背景图片.</param> 
         /// <returns>背景图片底色</returns> 
         private Color BackgroundImageColor(Image image)
         {
             Bitmap bmp = new Bitmap(image); 
             return bmp.GetPixel(0, 0);
         } 
        #endregion #endregion 
     }
}
